Skip to content

Include globals read only inside class bodies when pickling by value - #631

Open
draarivpatel-ui wants to merge 1 commit into
cloudpipe:masterfrom
draarivpatel-ui:fix-class-body-globals-extraction
Open

draarivpatel-ui wants to merge 1 commit into
cloudpipe:masterfrom
draarivpatel-ui:fix-class-body-globals-extraction

Conversation

@draarivpatel-ui

Copy link
Copy Markdown

Fixes #609.

Problem

A global that a by-value function reads only inside the body of a class it defines was not included in the pickled globals, so the depickled function raised NameError:

ns = {"__name__": "not_importable", "CONST": 42}  # not importable, so f is pickled by value
exec("def f():\n    class Rec:\n        q = CONST\n    return Rec.q\n", ns)
pickle.loads(cloudpickle.dumps(ns["f"]))()  # NameError: name 'CONST' is not defined

Cause

_extract_code_globals only collects names loaded with LOAD_GLOBAL, STORE_GLOBAL or DELETE_GLOBAL. Inside a class scope the compiler reads names with other opcodes:

  • class-body statements (and annotations before 3.14) use LOAD_NAME;
  • class-scoped type alias values (3.12+) and the class __annotate__ function (3.14+) use LOAD_FROM_DICT_OR_GLOBALS.

Fix

Add LOAD_NAME and LOAD_FROM_DICT_OR_GLOBALS (where available) to GLOBAL_OPS. Both consumers of _extract_code_globals already keep only names present in func.__globals__, so class-local names collected this way are dropped, and names shadowed by the class body still resolve to the class-local value at runtime.

Tests

Added test_class_body_reads_globals (including the shadowing case) and test_class_annotation_reads_globals. Full suite: 242 passed, 20 skipped, 2 xfailed.

_extract_code_globals only collected names loaded with LOAD_GLOBAL,
STORE_GLOBAL or DELETE_GLOBAL. Inside a class scope the compiler reads
names with LOAD_NAME (class body statements and annotations before
3.14) and LOAD_FROM_DICT_OR_GLOBALS (class-scoped type aliases and the
__annotate__ function on 3.14+), so a global referenced solely from a
class body inside a by-value function was missing from the pickled
globals and the depickled function raised NameError.

Closes cloudpipe#609
Copilot AI balanced review requested due to automatic review settings November 26, 2026 00:48

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Globals read only inside a class body defined in a by-value function are not pickled (NameError after loading)

2 participants